package com.micro.service.controller;

import com.micro.service.dao.RouteRepository;
import com.micro.service.model.ResponseMessage;
import com.micro.service.service.RefreshRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Create by Vic Xu on 2019/3/28
 */
@RestController
@RequestMapping(value = "/v1/refresh", produces = {"application/json;charset=UTF-8"})
public class RefreshRouteController {
    @Autowired
    RefreshRouteService refreshRouteService;
//    @Autowired
//    ZuulHandlerMapping zuulHandlerMapping;

    @Autowired
    private RouteRepository routeRepository;

    @Autowired
    private void init() {
        routeRepository.deleteAll();
        ZuulProperties.ZuulRoute route1 = new ZuulProperties.ZuulRoute();
        route1.setId("auth");
        route1.setPath("/auth/**");
        route1.setServiceId("oauth-server");
        route1.setCustomSensitiveHeaders(true);
        routeRepository.save(route1);

        ZuulProperties.ZuulRoute route2 = new ZuulProperties.ZuulRoute();
        route2.setId("consumer");
        route2.setPath("/consumer/**");
        route2.setServiceId("consumer-main");
        routeRepository.save(route2);
    }

    @GetMapping
    public ResponseMessage<String> refreshRoute() {
        refreshRouteService.refreshRoute();
        return ResponseMessage.success("refresh success");
    }

//    @GetMapping
//    public ResponseMessage<Map<String, Object>> watchCurrentRoute() {
//        Map<String, Object> handlerMap = zuulHandlerMapping.getHandlerMap();
//        return ResponseMessage.success(handlerMap);
//    }

}
